package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.GuessData;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.GuessRecsList;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ShenHao on 2016/12/12.
 */

public class Adapter_Rec2Detail extends RecyclerView.Adapter<Adapter_Rec2Detail.MyViewHolder> {

    public static final int HEAD_ID = 0;
    public static final int RECYLER_ID = 1;
    private Context context;
    private GuessData data;
    private List<GuessRecsList> list;
    private RecyclerView recyclerView;


    public Adapter_Rec2Detail(Context context, GuessData data) {
        this.context = context;
        this.data = data;
        list = new ArrayList<>();
        list.addAll(data.getRecs().getList());
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEAD_ID;
        } else {
            return RECYLER_ID;
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEAD_ID) {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.recomend_guess2_d_head, parent, false));
        } else {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.recomend_detail_listitem, parent, false));
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
        RecyclerView.LayoutManager manager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (position == 0) {
            holder.text_detail_tags.setText(data.getDetail().getIntro());


            holder.text_userNickname.setText(data.getUser().getNickname());
            holder.text_anchor_follwers.setText("已被"+data.getUser().getFollowers()/1000+"人关注");
            holder.text_anchor_ptitle.setText(data.getUser().getPersonDescribe());
            Picasso.with(context).load(data.getUser().getSmallLogo()).transform(new CircleTransform()).into(holder.image_smallLogo);
        } else {
            Picasso.with(context).load(data.getRecs().getList().get(position - 1).getCoverSmall()).into(holder.image_coverSmall);
            holder.text_listIntro.setText(data.getRecs().getList().get(position - 1).getIntro());
            holder.text_listplayTimes.setText(data.getRecs().getList().get(position - 1).getPlayTimes()/10000+"万");
            holder.text_listTitle.setText(data.getRecs().getList().get(position - 1).getTitle());
            holder.text_listTracks.setText(data.getRecs().getList().get(position - 1).getTracks()+"集");
        }
    }

    @Override
    public int getItemCount() {
        if (list.size() == 0) {
            return 1;
        }
        return list.size() + 1;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView text_detail_tags, text_userNickname, text_anchor_follwers, text_anchor_ptitle;
        private ImageView image_smallLogo, image_coverSmall;
        private TextView text_listTitle, text_listIntro, text_listplayTimes, text_listTracks;

        public MyViewHolder(View itemView) {
            super(itemView);

            text_anchor_follwers = (TextView) itemView.findViewById(R.id.recomend_anchor_follwers);
            text_anchor_ptitle = (TextView) itemView.findViewById(R.id.recomend_guess2_anchor_ptitle);
            text_detail_tags = (TextView) itemView.findViewById(R.id.recomend_guess2_detail_tag);
            text_userNickname = (TextView) itemView.findViewById(R.id.recomend_anchor_nickName);
            image_smallLogo = (ImageView) itemView.findViewById(R.id.recomend_anchor_small_Logo);

            //RecyclerView的item
            image_coverSmall = (ImageView) itemView.findViewById(R.id.recomend_detail_listcoverSmall);
            text_listIntro = (TextView) itemView.findViewById(R.id.recomend_detail_listIntro);
            text_listplayTimes = (TextView) itemView.findViewById(R.id.recomend_detail_listplayTimes);
            text_listTracks = (TextView) itemView.findViewById(R.id.recomend_detail_listTracks);
            text_listTitle = (TextView) itemView.findViewById(R.id.recomend_detail_listTitle);
        }
    }
}
